package com.mms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mms.entity.User;
import com.mms.entity.UserRole;
import com.mms.util.JdbcUtils;

/**
 * DAO组件，负责对数据库的user表执行CRUD
 * 
 * @author Administrator
 *
 */
public class UserMapperImpl implements UserMapper {

	@Override
	public User selectByUsernameAndPassword(String username, String md5Password) {
		String sql = "SELECT username, password, enabled, role, manager FROM `user` WHERE username = ? AND password = ?";
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		User user = null;
		try {
			conn = JdbcUtils.getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, username);
			stmt.setString(2, md5Password);
			rs = stmt.executeQuery();
			if (rs.next()) {
				user = new User();
				boolean enabled = rs.getBoolean("enabled");
				UserRole role = Enum.valueOf(UserRole.class, rs.getString("role"));
				String manager = rs.getString("manager");
				user.setEnabled(enabled);
				user.setRole(role);
				user.setManager(manager);
				user.setUsername(username);
				user.setPassword(md5Password);
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtils.close(rs, stmt, conn);
		}
		return user;
	}

	@Override
	public void insert(User newUser) {
		// 准备好sql语句
		String sql = "INSERT INTO `user`(username, password, enabled, role, manager) VALUES(?, ?, ?, ?, ?)";
		// 接下来编写千篇一律的jdbc代码
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			// 2. 使用DriverManager创建数据库连接，url写法：协议://主机名:端口号/要连接的数据库
			conn = JdbcUtils.getConnection();
			// 3. 基于数据库连接，创建语句对象
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, newUser.getUsername());
			stmt.setString(2, newUser.getPassword());
			stmt.setBoolean(3, newUser.isEnabled());
			stmt.setString(4, newUser.getRole().name());
			stmt.setString(5, newUser.getManager());
			// 4. 执行SQL语句，得到受影响的行数
			stmt.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtils.close(stmt, conn);
		}
	}

	@Override
	public void updateEnabledByUsername(String username, boolean enabled) {
		// 准备好sql语句
		String sql = "UPDATE `user` SET enabled = ? WHERE username = ?";
		// 接下来编写千篇一律的jdbc代码
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = JdbcUtils.getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setBoolean(1, enabled);
			stmt.setString(2, username);
			stmt.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtils.close(stmt, conn);
		}

	}
	
	@Override
	public void updatePasswordByUsername(String username, String newPassword) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql = "UPDATE `user` SET password = ? WHERE username = ?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, newPassword);
			stmt.setString(2, username);
			stmt.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtils.close(stmt, conn);
		}
	}

}
